// pages/dingdan/dingdan.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currtab: 0,
    swipertab: [{
        name: '普通订单',
        index: 0
      },
      {
        name: '预约订单',
        index: 1
      }
    ],
    all: [],
    fukuan: [],
    fahuo: [],
    shouhuo: [],
    num: 0,
    item: [],
    ip:[],
    alll:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

   
    console.log(options.id)
    this.setData({
      currtab: 0,
      index:options.id
    })
    this.onChecked();
    this.getDeviceInfo()

  },
  Product() {
    var that = this
    var car = []
    var carts = app.globalData.product //全部产品
    var sum = app.globalData.product_sum //产品总数
    console.log(carts)
    console.log(that.data.num)
    console.log(wx.getStorageSync('num'))
    for (var i = 0; i < wx.getStorageSync('num'); i++) { //全部订单个数
      console.log(i)
      for (var j = 0; j < sum; j++) {
        console.log(that.data.all[i].isisis)
        if (that.data.all[i].isisis == 0 && carts[j].product_code == that.data.all[i].product_code) {
          console.log(that.data.all[i].isisis)
          var item = {
            id: that.data.all[i].name, //订单名字
            name: carts[j].product_name, //产品名称
            image: carts[j].product_swiper_pic1, //产品图片
            num: that.data.all[i].sum, //订单数量
            price: that.data.all[i].actual_price, //订单价格
            all_price: that.data.all[i].all_price, //订单原价
            fukuan: that.data.all[i].fukuan, //是否付款
            fahuo: that.data.all[i].fahuo, //是否发货
            shouhuo: that.data.all[i].shouhuo, //收获
            cost: carts[j].pre_cost,
            transaction_no: that.data.all[i].transaction_no,
            tuihuo: that.data.all[i].tuihuo,
            istuihuo: that.data.all[i].istuihuo,
            currtab : 0
          }
          console.log(item)
          if(item != undefined){
            var cars = []
            if(that.data.fahuo == 1 && that.data.tuihuo == 0 && that.data.shouhuo == 1 && that.data.fukuan == 1||
              that.data.tuihuo == 1 && that.data.istuihuo == 1){
              car.push(item)
              console.log(car)
            }else{
              cars[0] = item
              for(var k = 1;k <= i ; k++){
                cars.push(car[k-1])
              }
              console.log(cars)
              car = cars
            }
          }
         
        
        }else if(that.data.all[i].isisis != 0 && i != that.data.ip[this.data.ip.length-1]){
            that.setData({
              ip: this.data.ip.concat(i)
            })

          console.log(this.data.ip)
          console.log(this.data.ip.length)
        }
      }

    }
    that.setData({
      all: car
    })
    app.globalData.all = that.data.all

  },
  onChecked() {
    //获取用户订单
    var that = this
    console.log(that.data.index)
    if(that.data.index == 1){
      // console.log(11111)
      
      let tableName = 'Order'
  
      let Query = new wx.BaaS.Query()
      // Query.compare('phone', '=', app.globalData.phone)
      let Product = new wx.BaaS.TableObject(tableName)
      Product.setQuery(Query).limit(1000).offset(0).find().then(res => {
        // success
        console.log(res)
        that.setData({
          all: res.data.objects, //用户全部订单数据
          alll:res.data.objects
        })
        that.setData({
          num: res.data.objects.length
        })
        wx.setStorageSync('num', res.data.objects.length)
        that.Product()
        console.log(res.data.objects.length)
      }, err => {
        // err
      })
    }else{
      // console.log(22222222222)
      var that = this
      let tableName = 'Order'
  
      let Query = new wx.BaaS.Query()
      Query.compare('phone', '=', app.globalData.phone)
      let Product = new wx.BaaS.TableObject(tableName)
      Product.setQuery(Query).limit(1000).offset(0).find().then(res => {
        // success
        console.log(res)
        that.setData({
          all: res.data.objects, //用户全部订单数据
          alll:res.data.objects
        })
        that.setData({
          num: res.data.objects.length
        })
        that.Product()
        wx.setStorageSync('num', res.data.objects.length)
        console.log(res.data.objects.length)
      }, err => {
        // err
      })
    }
   
  },
  /**
   * @Explain：选项卡点击切换
   */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },

  tabChange: function (e) {
    this.setData({
      currtab: e.detail.current
    })
    this.orderShow()
  },

  orderShow: function () {
    let that = this
    switch (this.data.currtab) {
      case 0:
        // that.allShow()
        break
      case 1:
        that.fukuanShow()
        console.log(111)
        break
      case 2:
        that.fahuoShow()
        break
      case 3:
        that.shouhuoShow()
    }
  },

  fukuanShow() {
    var that = this
    var fukuan = []
    var i = 0;
    var service_items = app.globalData.service_items
    var sum = app.globalData.service_items_sum
    console.log(this.data.ip.length)
    for (i; i < this.data.ip.length; i++) {
      var ip = that.data.ip[i]

      for (var f = 0; f < sum; f++) {
        
        if (that.data.ip.length != 0 && that.data.alll[parseInt(ip)].name == service_items[f].item_name) {
          // console.log(service_items[f])
          var item = {
            id: that.data.alll[that.data.ip[i]].name, //订单名字
            name: service_items[f].item_name, //产品名称
            image:service_items[f].item_pic, //产品图片
            // num: that.data.all[i].sum, //订单数量
            price: that.data.alll[that.data.ip[i]].actual_price, //订单价格
            all_price: that.data.alll[that.data.ip[i]].all_price, //订单原价
            fukuan: that.data.alll[that.data.ip[i]].fukuan, //是否付款
            fahuo: that.data.alll[that.data.ip[i]].fahuo, //是否发货
            shouhuo: that.data.alll[that.data.ip[i]].shouhuo, //收获
            // cost: carts[j].pre_cost,
            transaction_no: that.data.alll[that.data.ip[i]].transaction_no,
            tuihuo: that.data.alll[that.data.ip[i]].tuihuo,
            istuihuo: that.data.alll[that.data.ip[i]].istuihuo,
            time: that.data.alll[that.data.ip[i]].service_time,
            currtab :1,
            isxianshang:that.data.alll[that.data.ip[i]].isxianshang
          }
          app.globalData.service_items_pic = item.image
          app.globalData.service_items_cost = service_items[f].all_price
          var cars = []
          if(that.data.alll[that.data.ip[i]].fahuo == 1 && that.data.alll[that.data.ip[i]].tuihuo == 0 && that.data.alll[that.data.ip[i]].shouhuo == 1 && that.data.alll[that.data.ip[i]].fukuan == 1||
            that.data.alll[that.data.ip[i]].tuihuo == 1 && that.data.alll[that.data.ip[i]].istuihuo == 1){
              fukuan.push(item)
            console.log(cars)
          }else {
            cars[0] = item
            for(var k = 1;k <=i ; k++){
              cars.push(fukuan[k-1])
            }
            console.log(cars)
            fukuan = cars
          }
        }
      }

    }
    that.setData({
      fukuan: fukuan
    })

  },

  fahuoShow() {
    var that = this
    var fahuo = []
    var i = 0;
    for (i; i < this.data.sum; i++) {
      if (that.data.all[i].fukuan == 1 && that.data.all[i].fahuo == 1 && that.data.all[i].shouhuo == 0) {
        fahuo.push(that.data.all[i])
      }
    }
    that.setData({
      fahuo: fahuo
    })
  },

  shouhuoShow() {
    var that = this
    var shouhuo = []
    var i = 0;
    for (i; i < this.data.sum; i++) {
      if (that.data.all[i].fukuan == 1 && that.data.all[i].fahuo == 1 && that.data.all[i].shouhuo == 0) {
        shouhuo.push(that.data.all[i])
      }
    }
    that.setData({
      shouhuo: shouhuo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成

  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },
  tiaozhuan: function (e) {
    console.log(e.currentTarget.dataset.postid)
    var postid = e.currentTarget.dataset.postid;
    console.log(postid)
    console.log(this.data.all)
    var id = this.data.all[postid].transaction_no
    console.log(id)
    app.navigateToBuy(this.data.all[postid].transaction_no)
  },
  tiaozhuanfuwu:function(e){
    console.log(111111111111)
    console.log(e.currentTarget.dataset.postid)
    var postid = e.currentTarget.dataset.postid;
    console.log(postid)

    var id = this.data.fukuan[postid].transaction_no
    console.log(id)
    app.navigateToBuy(this.data.fukuan[postid].transaction_no)
  }
})